<template>
  <div class="container">
    <div class="loginLogo">
      <el-image :src="require('@/assets/img/yinke.png')" style="width: 200px;margin-left:12%;margin-top: 0.5%;" fit="contain"></el-image>
      <hr style="border-top: 1px solid #eeeeee;" />
    </div>
    <div class="loginPhoto">
      <el-image :src="require('@/assets/img/banner2.jpg')" style="width: 450px;" fit="contain"></el-image>
    </div>
    <div class="yinkeTextLogin">
      <div align='center'>{{$t('银科数安防火墙系统')}}</div>
    </div>
    <div class="loginFormHelishi">
      <el-form label-width="" :model="ruleForm" ref="ruleForm" :rules="loginRules" class="loginFormItem">
        <el-form-item :label="$t('用户名')" prop="username">
          <el-input v-emoji v-model="ruleForm.username" size="medium" :placeholder="$t('用户名')"></el-input>
        </el-form-item>
        <el-form-item :label="$t('密码')" prop="password">
          <el-input v-emoji type="password" size="medium" :placeholder="$t('密码')" v-model="ruleForm.password" @keyup.enter.native="submitForm(ruleForm)"></el-input>
        </el-form-item>
        <el-button type="warning" class="loginButton" :loading="loading" size="small" @click.native.prevent="submitForm(ruleForm)">{{$t('登录')}}</el-button>
        <div style="text-align: right;margin-top: 2px;">
          <el-link :underline="false" style="color: #666;font-size: 12px;" @click="userAuthLoginClick">{{$t('用户认证')}}<i class="el-icon-arrow-right el-icon--right"></i> </el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { setRoleList } from '@/utils/auth'
import { getMenu } from '@/api/menu'
export default {
  name: 'LoginHelishi',
  data () {
    const validateUsername = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入用户名')))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入密码')))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      loading: false,
      loginRules: {
        username: [{ required: false, trigger: 'blur', validator: validateUsername }],
        password: [{ required: false, trigger: 'blur', validator: validatePassword }]
      }
    }
  },
  methods: {
    submitForm (ruleForm) {
      let _that = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', ruleForm).then(res => {
            if (res.status === 0) {
              if (res.info[0].ukey !== 0) {
                this.OnLoadRead().then(r => {
                  setTimeout(function () {
                    _that.Arm_Enum_Read([res.info[0], _that.goMenu, _that.colseLoading])
                  }, 100)
                })
              } else {
                this.$store.dispatch('user/saveInfo', res.info[0]).then(() => {
                  getMenu({ roleID: res.info[0].roleID }).then((response) => {
                    if (response.status === 0) {
                      setRoleList(response.url_list)
                       this.$nextTick(() => {
                        this.loading = false
                        if (res.info[0].password_update_time === 0) {
                          this.$router.push('/changePassword')
                        } else {
                          this.$router.push('/')
                        }
                      })
                    }
                  }).catch(e => {
                    this.loading = false
                  })
                })
              }
              // this.$router.push('/network/physical/interface')
            } else {
              this.loading = false
            }
          }).catch(e => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
        }
      })
    },
    goMenu (info, status) {
      if (status !== 0) {
        this.loading = false
        this.$store.dispatch('user/logout')
        this.$messages.error(this.$t('UkeyAgain'))
      } else {
        this.$store.dispatch('user/saveInfo', info).then(() => {
          getMenu({ role: info.role }).then((response) => {
            if (response.status === 0) {
              setRoleList(response.url_list)
              this.$nextTick(() => {
                this.loading = false
                if (info.password_update_time === 0) {
                  this.$router.push('/changePassword')
                } else {
                  this.$router.push('/')
                }
              })
            }
          }).catch(e => {
            this.loading = false
          })
        })
      }
    },
    colseLoading () {
      this.loading = false
    },
    userAuthLoginClick () {
      const hrefUrl = window.location.href.match(/(http:\/\/|https:\/\/)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/)
      const url = hrefUrl[0] + ':8183/#/authLogin'
      window.open(url)
    }
  }
}
</script>

<style scoped>
.container {
  position: relative;
  width: 100%;
  height: 100vh;
  background-size: cover;
  box-sizing: border-box;
  z-index: 1;
  background-color: rgb(255, 255, 255, 1);
}
.loginFormHelishi {
  position: absolute;
  left: calc(53vw + 100px);
  top: 40%;
  transform: translate(-50%, -50%);
  /* width: 400px; */
  text-align: center;
  box-shadow: 0 0 6px rgb(0 0 0 / 10%);
  border: 1px solid #ddd;
  z-index: 1001;
  /* border: 2px solid rgba(255, 255, 255, 0.6); */
  /* background-color: rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(10px); */
  background-color: rgba(255, 255, 255, 1);
}
.yinkeTextLogin {
  position: absolute;
  left: calc(53vw + 100px);
  transform: translate(-50%, -50%);
  top: 17%;
  z-index: 1001;
  color: #333333;
  line-height: 1.1;
  font-size: 36px;
  width: 350px;
}
.loginFormItem {
  margin: 20px 20px 15px;
  width: 220px;
}
.loginLogo {
  position: absolute;
  width: 100%;
  z-index: 1001;
  border-bottom: 1px;
}
.loginPhoto {
  position: absolute;
  left: 8%;
  z-index: 1001;
  top: 10%;
}
.loginButton {
  width: 100%;
  margin-top: 10px;
  border: none;
  z-index: 10;
}
.textCompany {
  position: absolute;
  z-index: 1001;
  transform: translate(-50%, -50%);
  left: 50%;
  bottom: 0.1%;
  color: #337ab7;
}
</style>
<style lang='scss'>
.loginFormHelishi {
  .el-form-item__label {
    line-height: 25px;
    font-size: 14px;
  }
  .el-form-item {
    margin-bottom: 7px;
  }
}
</style>
